home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GameKit / Examples / PacMan / PacManPreferencesBrain.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  125 lines

  1.  
  2. #import "PacManPreferencesBrain.h"
  3. #import "PacManView.h"            // Need to send it the new -setScale: method
  4. #import <stdio.h>
  5. #import <string.h>
  6.  
  7. // Size of the play window when the game comes up if no default is set.
  8. #define DEFAULTscale 1        // default is a 1 or 2
  9.  
  10. // The screen must be at least this size to use the "Big" option.
  11. // Technically, the big window _does_ fit on 800x600, but you have to
  12. // play with the window positioning, and it looks kind of ugly when
  13. // you're done.  Thus I require the 1024x768 or larger settings.
  14. // If you've got an 800x600 screen and _really_ want this option,
  15. // then change these numbers...but it's probably not worth it.
  16. #define MINWIDTHFORBIG 900.0
  17. #define MINHEIGHTFORBIG 650.0
  18.  
  19. static BOOL screenTooSmall(id gameScreen)
  20. { // return YES if screen is too small for big size option, NO otherwise
  21.     const NXScreen *theScreen;
  22.     theScreen = [[gameScreen window] screen]; // screen with game window
  23.     if (!theScreen) theScreen = [NXApp mainScreen]; // if window not on screen
  24.     if ((NX_WIDTH(&(theScreen->screenBounds)) < MINWIDTHFORBIG) ||
  25.         (NX_HEIGHT(&(theScreen->screenBounds)) < MINHEIGHTFORBIG)) return YES;
  26.     return NO;
  27. }
  28.  
  29. @implementation PacManPreferencesBrain
  30.  
  31. // Methods for the Preference we add (the screen size "scale", int: 1 or 2)
  32. //  -(int)scale to query, - setScale:(int)newScale to set programmatically,
  33. //  and - scaleChange:sender to change from the panel's controls.
  34. - (int)scale { return scale; }
  35.  
  36. - setScale:(int)newScale
  37. {    // keep the value in the accepted range
  38.     // you should follow up any call to this method with a call to the
  39.     // gamescreen's -setScale: method -- I don't call it from here!
  40.     if (screenTooSmall(gameScreen)) newScale = 1;
  41.     if ((newScale < 1) || (newScale > 2)) return self;
  42.     scale = newScale;
  43.     return self;
  44. }
  45.  
  46. - scaleChange:sender
  47. {    // sender must be a Matrix of radio buttons with the tags 1=small, 2=big
  48.     scale = [[sender selectedCell] tag];
  49.     if (screenTooSmall(gameScreen)) scale = 1;
  50.     [gameScreen setScale:scale];
  51.     return self;
  52. }
  53.  
  54. - readDefaults:sender        // get preferences from defaults database
  55. {    // This shows how to add other prefs values beyond what the GameKit
  56.     // normally provides *and* how to override GameKit restrictions on
  57.     // the size of these variables (speed in this case; GameKit restricts
  58.     // it to the range 0-2, but I want 0-3 for PacMan.)
  59.     int speedTemp = getIntPreference("GameSpeed", 0, 3, 1); // load w/my MAX
  60.     scale = getIntPreference("Scale", 1, 2, DEFAULTscale); // a new pref
  61.     if (screenTooSmall(gameScreen)) scale = 1; // keep with screen size.
  62.     [super readDefaults:sender]; // get the GameKit versions
  63.     speed = speedTemp;    // override the GameKit restrictions
  64.     return self;
  65. }
  66.  
  67. - writeDefaults:sender        // save preferences in defaults database
  68. {
  69.     [super writeDefaults:sender];
  70.     // don't need to override speed here, just add the new pref.
  71.     putIntPreference("Scale",    scale);
  72.     return self;
  73. }
  74.  
  75. - revert:sender    // return to default values
  76. {
  77.     [super revert:sender];
  78.     scale = DEFAULTscale;    // this is the default value of the scale
  79.     return [self preferences:self]; // update the panel
  80. }
  81.  
  82. - refresh
  83. {
  84.     [super refresh]; // puts current prefs values into the panel
  85.     // disable big/small if screen too small and turn on the right radio switch
  86.     [scaleMatrix setEnabled:!screenTooSmall(gameScreen)];
  87.     [scaleMatrix selectCellWithTag:scale];
  88.     return self;
  89. }
  90.  
  91. // The next three methods are added to load specific pictures into the
  92. // background...  the pix must be in the app wrapper.  The methods all
  93. // just ask the GameView to load the background image and then display it.
  94.  
  95. - back1:sender { return [gameScreen loadAnImage:"Gradation.eps"]; }
  96. - back2:sender { return [gameScreen loadAnImage:"lush.tiff"]; }
  97. - back3:sender { return [gameScreen loadAnImage:"Sunset.tiff"]; }
  98.  
  99. // This one sets the background to a solid black.  It's here by
  100. // user request, even though it is functionally redundant.  That's
  101. // why I put in the alert panel.  If people get used to dragging colors
  102. // into game windows -- and it always works in the gamekit -- then I
  103. // won't have to stick this into every game I make; instead they'll
  104. // try the drag/drop first, rather than looking for a menu buried on
  105. // the prefs panel.  :-)
  106. - solidBlack:sender
  107. {    // The user can get this by dragging a color chip into the
  108.     // game window, but this isn't completely obvious, so we
  109.     // provide this.  We'll pop up an alert the first time around
  110.     // to let them know about drag and drop...
  111.     NXPoint tempPoint = { 0.0, 0.0 }; // for temp use...
  112.     if (alert && !getBOOLPreference("KnowDrag", NO)) {
  113.         NXRunAlertPanel(
  114.                 [strings valueForStringKey:"DidjaKnow"],
  115.                 [strings valueForStringKey:"YouCanDrag"],
  116.                 [strings valueForStringKey:"Neato"], NULL, NULL);
  117.         putBOOLPreference("KnowDrag", YES);
  118.     }
  119.     // now, simulate drag of a black chip into the gameview:
  120.     [gameScreen acceptColor:NX_COLORBLACK atPoint:&tempPoint];
  121.     return self;
  122. }
  123.  
  124. @end
  125.